//
//  Debug_UIViewController_Extension.swift
//  APSwift
//
//  Created by AbnerPei on 2021/12/23.
//

import UIKit

extension UIViewController {
    
    public func pushToNext(vcTitles: [String], vcNames: [String], at indexPath: IndexPath) {
        guard vcTitles.count > indexPath.row, vcTitles.count == vcNames.count else {
            assert(false, "⚠️ 请注意：vcTitles、vcNames（其一or两者）配置有问题")
            return
        }
        
        /// 参考：[28.Swift中的命名空间](https://www.jianshu.com/p/00e89101622a)
        guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            assert(false, "⚠️ 请注意：获取命名空间失败")
            return
        }
        
        let vcName = vcNames[indexPath.row]
        var vcCls: AnyClass?
        
        if !vcName.contains("_OC_") {
            guard let nextCls: AnyObject.Type = NSClassFromString("\(nameSpace).\(vcNames[indexPath.row])") else {
                assert(false, "⚠️ 请注意：无法更具vc名字字符串获取对应的vc")
                return
            }
            guard let nextVC = nextCls as? UIViewController.Type else{
                assert(false, "⚠️ 请注意：未能成功转换成UIViewController类型")
                return
            }
            vcCls = nextVC
        } else {
            vcCls = NSClassFromString(vcName)
        }
        
        guard let vcClass = vcCls as? UIViewController.Type else { return }
        
        let vc = vcClass.init()
        vc.title = vcTitles[indexPath.row]
        vc.view.backgroundColor = .white
        navigationController?.pushViewController(vc, animated: true)
    }
    
    public func pushToNext(vcTitles: [String], vcClasses: [UIViewController.Type], at indexPath: IndexPath) {
        guard vcTitles.count > indexPath.row, vcTitles.count == vcClasses.count else {
            assert(false, "⚠️ 请注意：vcTitles、vcClasses（其一or两者）配置有问题")
            return
        }
        let nextVCCLS = vcClasses[indexPath.row]
        let vc = nextVCCLS.init()
        vc.title = vcTitles[indexPath.row]
        vc.view.backgroundColor = .white
        navigationController?.pushViewController(vc, animated: true)
    }
}

public func APPrint() {
    print("\n")
}


//!!!: 错误的获取命名空间
//extension String {
//    public func toVC() -> UIViewController? {
//        guard let cls: AnyObject.Type = NSClassFromString(self) else { return nil }
//        guard let vc = cls as? UIViewController.Type else { return nil }
//        return vc.init()
//    }
//}

//!!!: 获取命名空间
extension String {
    public func toVC() -> UIViewController? {
        guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            assert(false, "⚠️ 请注意：获取命名空间失败")
            return nil
        }
        guard let cla: AnyObject.Type = NSClassFromString("\(nameSpace).\(self)") else {
            assert(false, "⚠️ 请注意：无法根据vc名字字符串获取对应的vc")
            return nil
        }
        guard let vc = cla as? UIViewController.Type else{
            assert(false, "⚠️ 请注意：未能成功转换成UIViewController类型")
            return nil
        }
        return vc.init()
    }
}
